package com.gm.app.aspect;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import com.gm.app.model.Address;
import com.gm.app.model.City;
import com.gm.app.model.Country;
import com.gm.app.model.District;
import com.gm.app.model.Place;
import com.gm.app.model.Region;
import com.gm.app.model.Street;

privileged aspect PlaceFinders {

	public static TypedQuery<Place> Place.findPlacesByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = Place.entityManager();
		TypedQuery<Place> q = em
				.createQuery(
						"SELECT o FROM Place AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						Place.class);
		q.setParameter("description", description);
		return q;
	}

	public static TypedQuery<Country> Country.findCountrysByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = Country.entityManager();
		TypedQuery<Country> q = em
				.createQuery(
						"SELECT o FROM Country AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						Country.class);
		q.setParameter("description", description);
		return q;
	}

	public static TypedQuery<Region> Region.findRegionsByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = Region.entityManager();
		TypedQuery<Region> q = em
				.createQuery(
						"SELECT o FROM Region AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						Region.class);
		q.setParameter("description", description);
		return q;
	}

	public static TypedQuery<City> City.findCitysByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = City.entityManager();
		TypedQuery<City> q = em
				.createQuery(
						"SELECT o FROM City AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						City.class);
		q.setParameter("description", description);
		return q;
	}

	public static TypedQuery<District> District.findDistrictsByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = District.entityManager();
		TypedQuery<District> q = em
				.createQuery(
						"SELECT o FROM District AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						District.class);
		q.setParameter("description", description);
		return q;
	}

	public static TypedQuery<Street> Street.findStreetsByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = Street.entityManager();
		TypedQuery<Street> q = em
				.createQuery(
						"SELECT o FROM Street AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						Street.class);
		q.setParameter("description", description);
		return q;
	}

	public static TypedQuery<Address> Address.findAddressesByDescriptionStartsWith(
			String description) {
		if (description == null || description.length() == 0)
			throw new IllegalArgumentException(
					"The description argument is required");
		description = description.replace('*', '%');
		if (description.charAt(description.length() - 1) != '%') {
			description = description + "%";
		}
		EntityManager em = Address.entityManager();
		TypedQuery<Address> q = em
				.createQuery(
						"SELECT o FROM Address AS o WHERE LOWER(o.description) LIKE LOWER(:description)",
						Address.class);
		q.setParameter("description", description);
		return q;
	}
}
